class Person:

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def print_name(self):
        print(self.name)


person = Person('张三',30)
print(getattr(person,'name'))
print(getattr(person,'gender','男'))

setattr(person,'gender','女')
print(person.gender)

# 适用场景
attr = 'name'
print(getattr(person,attr))